<?php
namespace Admin\Controller;

use Admin\Controller\BaseController;
use Think\Controller;

class StatisticsUserController extends BaseController
{
    public $obj;
    public $temp_obj;
    public $local_id;

    public function __construct()
    {
        parent::__construct();
        $this->local_id = session('userInfo')['local_id'];
    }

    /**
     * 按天统计
     */
    public function day()
    {
        $this->_arr['p'] = I('p', 1, 'intval');
        $this->_arr['select'] = I('select');
        $this->_arr['value'] = trim(I('value'));
        $this->_arr['begintime'] = I('begintime');
        $this->_arr['endtime'] = I('endtime');
        $where = 'local_id='.$this->local_id;
        if($this->_arr['begintime']){
            $where .= " and `date` >= '".$this->_arr['begintime']."'";
        }
        if($this->_arr['endtime']){
            $where .= " and `date` <= '".$this->_arr['endtime']."'";
        }

        if($this->_arr['select'] && $this->_arr['value']){
            switch($this->_arr['select']){
                case 'mobile' :
                    $where .= ' and mobile = '.$this->_arr['value'];
                    break;
                case 'defualt_name' :
                    $where .= " and defualt_name like '".$this->_arr['value']."%'";
                    break;
                case 'nickname' :
                    $where .= " and nickname like '".$this->_arr['value']."%'";
                    break;
            }
        }
        $obj = M('statistical_member_day');
        $data = $obj->where($where)->order('date desc')->page($this->_arr['p'] . ','.$this->page_num)->select();
        $count = $obj->where($where)->count();// 查询满足要求的总记录数
        //会员总数
        $this->_arr['sumUserNum'] = $obj->query("SELECT COUNT(*) AS num FROM sdb_b2c_members m
  INNER JOIN (SELECT * FROM sdb_b2c_store_user a WHERE local_id = $this->local_id)  s_u ON m.member_id = s_u.member_id")[0]['num'];
        //消费 会员总数
        $dataNum = $obj->query("SELECT COUNT(*) as num FROM (SELECT id FROM `sdb_statistical_member_day` WHERE `local_id` = $this->local_id GROUP BY member_id ) a");

        $this->_arr['userNum'] = $dataNum[0]['num'];
        $Page = new \Think\Page($count, $this->page_num);// 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();// 分页显示输出
        $this->_arr['page'] = $show;// 赋值分页输出
        $this->_arr['list'] = $data;
        $this->_showDisplay();

    }

    /**
     * 月天统计
     */
    public function month()
    {
        $this->_arr['p'] = I('p', 1, 'intval');
        $this->_arr['select'] = I('select');
        $this->_arr['value'] = trim(I('value'));
        $this->_arr['year'] = I('year');
        $this->_arr['month'] = I('month');
        $where = 'local_id='.$this->local_id;
        $this->_arr['monthData'] = ['01','02','03','04','05','06','07','08','09','10','11','12'];

        if($this->_arr['year'] && $this->_arr['month']){
            $date = $this->_arr['year'].'-'.$this->_arr['month'];
            $where .= " and `date` = '".$date."'";
        }elseif($this->_arr['year']){
            $where .= " and `date` >= '".$this->_arr['year']."-01' and `date` <='".$this->_arr['year']."-12'";
        }elseif($this->_arr['month']){
            $where .= " and `date` = '".date("Y")."-".$this->_arr['month']."'";
        }
        //dump($where);
        if($this->_arr['select'] && $this->_arr['value']){
            switch($this->_arr['select']){
                case 'mobile' :
                    $where .= ' and mobile = '.$this->_arr['value'];
                    break;
                case 'defualt_name' :
                    $where .= " and defualt_name like '".$this->_arr['value']."%'";
                    break;
                case 'nickname' :
                    $where .= " and nickname like '".$this->_arr['value']."%'";
                    break;
            }
        }
        $obj = M('statistical_member_mouth');
        $data = $obj->where($where)->order('date desc')->page($this->_arr['p'] . ','.$this->page_num)->select();
        $count = $obj->where($where)->count();// 查询满足要求的总记录数
        //会员总数
        $this->_arr['sumUserNum'] = $obj->query("SELECT COUNT(*) AS num FROM sdb_b2c_members m
  INNER JOIN (SELECT * FROM sdb_b2c_store_user a WHERE local_id = $this->local_id)  s_u ON m.member_id = s_u.member_id")[0]['num'];
        //消费 会员总数
        $dataNum = $obj->query("SELECT COUNT(*) as num FROM (SELECT id FROM `sdb_statistical_member_day` WHERE `local_id` = $this->local_id GROUP BY member_id ) a");

        $this->_arr['userNum'] = $dataNum[0]['num'];
        $Page = new \Think\Page($count, $this->page_num);// 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();// 分页显示输出
        $this->_arr['page'] = $show;// 赋值分页输出
        $this->_arr['list'] = $data;
        $this->_showDisplay();

    }

    /**
     * 新增会员统计
     */
    function  newUser(){
        $this->_arr['p'] = I('p', 1, 'intval');
        $this->_arr['begintime'] = I('begintime');
        $this->_arr['endtime'] = I('endtime');
        $where = 'local_id='.$this->local_id;
        if($this->_arr['begintime']){
            $where .= " and `date` >= '".$this->_arr['begintime']."'";
        }
        if($this->_arr['endtime']){
            $where .= " and `date` <= '".$this->_arr['endtime']."'";
        }
        if(empty($this->_arr['endtime']) && empty($this->_arr['begintime'])){
            $where .= " and `date`=".date('Y-m-d');
        }

        $obj = M('statistical_member_new');
        $data = $obj->where($where)->order('date desc')->page($this->_arr['p'] . ','.$this->page_num)->select();

        $count = $obj->where($where)->count();// 查询满足要求的总记录数
        //上月新增会员
        $month = date('m');
        if($month[0] == '0'){
            $frontMonth = date('Y').'-0'.($month[1] - 1);
        }else{
            $frontMonth = date('Y').($month - 1);
        }
        //上月新增会员总数
        $dataNum = M()->query("SELECT SUM(num) AS num FROM `sdb_statistical_member_new` WHERE local_id=".$this->local_id." AND `date`>='".$frontMonth."-01' AND `date`<='".$frontMonth."-".date('t',strtotime($frontMonth))."'");

        $this->_arr['userNum'] = $dataNum[0]['num'];
        $Page = new \Think\Page($count, $this->page_num);// 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();// 分页显示输出
        $this->_arr['page'] = $show;// 赋值分页输出
        $this->_arr['list'] = $data;
        $this->_showDisplay();
    }



    public function __destruct()
    {
      unset($this->local_id);
    }


}